//
//  NSObject+MagicalModel.h
//  DBManagerDemo
//
//  Created by CRY on 16/6/30.
//  Copyright © 2016年 CRY. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "TRDefine.h"

@interface NSObject (Magical)
/**
 *  Set up primary key.
 */
- (NSString *)tr_setPrimaryKey;
/**
 *  Save data to local.
 */
- (void)tr_saveToPersistentStoreAndWaitWithFinished:(FinishedBlock)finished;
/**
 *  Find all data from database.
 */
+ (NSArray *)tr_findAll;

+ (NSArray *)tr_findAllOrderedByAttribute:(NSString *)attribute
                                ascending:(BOOL)ascending;
+ (NSArray *)tr_findAllOrderedByAttribute:(NSString *)attribute
                                ascending:(BOOL)ascending
                                    limit:(NSInteger)limit
                                   offset:(NSInteger)offset;
/**
 *  Find data from database by some conditions. Those conditions whose type are NSSttring are stored in NSArray.
 */
+ (NSArray *)tr_findByParameters:(NSArray *)params;

+ (NSArray *)tr_findByParameters:(NSArray *)params
              orderedByAttribute:(NSString *)attribute
                       ascending:(BOOL)ascending;

+ (NSArray *)tr_findByParameters:(NSArray *)params
              orderedByAttribute:(NSString *)attribute
                       ascending:(BOOL)ascending
                           limit:(NSInteger)limit
                          offset:(NSInteger)offset;
/**
 *  Find data from database by one condition. 
 */
+ (NSArray *)tr_findByAttribute:(NSString *)attribute
                          value:(NSString *)value;
/**
 *  Find a data from database by order data with a attribute.
 */
+ (instancetype)tr_findFirstOrderedByAttribute:(NSString *)attribute
                                     ascending:(BOOL)ascending;

- (void)tr_deleteWithFinished:(FinishedBlock)finished;

@end
